package org.chen.hotel.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.chen.common.hotel.ConvenientAttraction;
import org.chen.common.hotel.vo.ConvenientAttractionVO;
import org.chen.hotel.mapper.ConvenientAttractionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ConvenientAttractionService {

    @Autowired
    private ConvenientAttractionMapper attractionMapper;

    @Autowired
    private CityService cityService;

    /**
     * 获取所有启用的便捷景点，按排序值升序、创建时间降序排序
     */
    public List<ConvenientAttractionVO> getAllActiveAttractions() {
        QueryWrapper<ConvenientAttraction> wrapper = new QueryWrapper<>();
        wrapper.eq("is_active", 1)
                .orderByAsc("sort_order")
                .orderByDesc("create_time");

        List<ConvenientAttraction> attractions = attractionMapper.selectList(wrapper);
        return attractions.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    /**
     * 根据城市ID获取便捷景点，按排序值升序、创建时间降序排序
     */
    public List<ConvenientAttractionVO> getAttractionsByCityId(Integer cityId) {
        QueryWrapper<ConvenientAttraction> wrapper = new QueryWrapper<>();
        wrapper.eq("city_id", cityId)
                .eq("is_active", 1)
                .orderByAsc("sort_order")
                .orderByDesc("create_time");

        List<ConvenientAttraction> attractions = attractionMapper.selectList(wrapper);
        return attractions.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    /**
     * 根据ID获取便捷景点
     */
    public ConvenientAttraction getAttractionById(Long id) {
        if (id == null) {
            return null;
        }

        QueryWrapper<ConvenientAttraction> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id)
                .eq("is_active", 1);

        return attractionMapper.selectOne(wrapper);
    }

    /**
     * 根据名称模糊查询便捷景点
     */
    public List<ConvenientAttractionVO> getAttractionsByNameLike(String name) {
        QueryWrapper<ConvenientAttraction> wrapper = new QueryWrapper<>();
        wrapper.like("name", name)
                .eq("is_active", 1)
                .orderByAsc("sort_order")
                .orderByDesc("create_time");

        List<ConvenientAttraction> attractions = attractionMapper.selectList(wrapper);
        return attractions.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    /**
     * 转换为VO，包含城市名称
     */
    private ConvenientAttractionVO convertToVO(ConvenientAttraction attraction) {
        ConvenientAttractionVO vo = new ConvenientAttractionVO();
        vo.setId(attraction.getId());
        vo.setName(attraction.getName());
        vo.setLatitude(attraction.getLatitude());
        vo.setLongitude(attraction.getLongitude());
        vo.setCityId(attraction.getCityId());
        vo.setDescription(attraction.getDescription());

        // 关联查询城市名称
        try {
            var city = cityService.getCityById(attraction.getCityId());
            if (city != null) {
                vo.setCityName(city.getName());
            }
        } catch (Exception e) {
            log.warn("获取城市信息失败, cityId: {}", attraction.getCityId(), e);
        }

        return vo;
    }

    /**
     * 保存便捷景点
     */
    public boolean saveAttraction(ConvenientAttraction attraction) {
        return attractionMapper.insert(attraction) > 0;
    }

    /**
     * 更新便捷景点
     */
    public boolean updateAttraction(ConvenientAttraction attraction) {
        return attractionMapper.updateById(attraction) > 0;
    }

    /**
     * 逻辑删除便捷景点
     */
    public boolean deleteAttraction(Long id) {
        ConvenientAttraction attraction = new ConvenientAttraction();
        attraction.setId(id);
        attraction.setIsActive(false);
        return attractionMapper.updateById(attraction) > 0;
    }

    /**
     * 获取指定城市的景点数量
     */
    public long getAttractionCountByCityId(Integer cityId) {
        QueryWrapper<ConvenientAttraction> wrapper = new QueryWrapper<>();
        wrapper.eq("city_id", cityId)
                .eq("is_active", 1);
        return attractionMapper.selectCount(wrapper);
    }

    /**
     * 检查景点名称是否已存在
     */
    public boolean existsByName(String name, Integer cityId) {
        QueryWrapper<ConvenientAttraction> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name)
                .eq("city_id", cityId)
                .eq("is_active", 1);
        return attractionMapper.selectCount(wrapper) > 0;
    }
}